package com.kakarote.oa.controller;


import cn.hutool.core.util.StrUtil;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.ApiExplain;
import com.kakarote.core.common.Result;
import com.kakarote.core.common.enums.FieldEnum;
import com.kakarote.oa.common.ActionRecordUtil;
import com.kakarote.oa.entity.BO.ExamineFieldBO;
import com.kakarote.oa.entity.PO.OaExamineField;
import com.kakarote.oa.service.IOaExamineFieldService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义字段表 前端控制器
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-06-22
 */
@RestController
@RequestMapping("/oaExamineField")
public class OaExamineFieldController {

    @Autowired
    private IOaExamineFieldService examineFieldService;

    /**
     * @author wyq
     * 查询新增或编辑字段
     */
    @ApiOperation("查询新增或编辑字段")
    @PostMapping("/queryField/{categoryId}")
    public Result<List> queryField(@PathVariable("categoryId") Long categoryId,
                                   @RequestParam(value = "type", required = false) String type,
                                   @RequestParam(value = "needHidden", required = false) Boolean needHidden
    ) {
        if (StrUtil.isNotEmpty(type)) {
            return Result.ok(examineFieldService.queryField(categoryId, needHidden));
        }
        return Result.ok(examineFieldService.queryFormPositionField(categoryId, needHidden));
    }

    @ApiOperation("保存自定义字段")
    @PostMapping("/saveField")
    @OperateLog(type = OperateTypeEnum.SETTING, behavior = BehaviorEnum.UPDATE)
    public Result saveField(@RequestBody ExamineFieldBO examineFieldBO) {
        OperationLog operationLog = examineFieldService.saveField(examineFieldBO);
        return OperationResult.ok(operationLog);
    }

    @ApiExplain("保存默认字段")
    @PostMapping("/saveDefaultField")
    public Result saveDefaultField(@RequestParam("categoryId") Long categoryId) {
        examineFieldService.saveDefaultField(categoryId);
        return Result.ok();
    }

    @ApiOperation("修改自定义字段")
    @PostMapping("/updateFieldCategoryId")
    public Result<Boolean> updateFieldCategoryId(@RequestParam("newCategoryId") Long newCategoryId, @RequestParam("oldCategoryId") Long oldCategoryId) {
        return Result.ok(examineFieldService.updateFieldCategoryId(newCategoryId, oldCategoryId));
    }

    @ApiOperation("查询人员控件")
    @PostMapping("/queryUserField/{categoryId}")
    public Result<List<OaExamineField>> queryUserField(@PathVariable(value = "categoryId") Long categoryId) {
        List<OaExamineField> oaExamineFields = examineFieldService.queryField(categoryId, true);
        List<Integer> userField = new ArrayList<Integer>() {{
            add(FieldEnum.USER.getType());
        }};
        if (oaExamineFields.isEmpty()) {
            return Result.ok();
        } else {
            List<OaExamineField> fieldList = oaExamineFields.stream()
                    .filter(e -> userField.contains(e.getType())).filter(e -> e.getIsNull() == 1)
                    .peek(e -> e.setFieldName(StrUtil.toCamelCase(e.getFieldName())))
                    .collect(Collectors.toList());
            return Result.ok(fieldList);
        }

    }

    @ApiOperation("查询审批全部字段")
    @PostMapping("/queryFieldList/{categoryId}")
    public Result<List<OaExamineField>> queryFieldList(@PathVariable(value = "categoryId") Long categoryId) {
        List<OaExamineField> oaExamineFields = examineFieldService.queryField(categoryId, true);

        for (OaExamineField oaExamineField : oaExamineFields) {
            Integer type = oaExamineField.getType();
            if (FieldEnum.EXAMINE_CAUSE.getType().equals(type) || FieldEnum.BUSINESS_CAUSE.getType().equals(type)) {
                oaExamineField.setFieldExtendList(ActionRecordUtil.queryOaExamineFieldExtend(type));
            }
        }
        oaExamineFields.removeIf(e -> e.getType().equals(FieldEnum.DESC_TEXT.getType()));
        return Result.ok(oaExamineFields);


    }
}

